package com.example.lin.homework_yjj_login.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.lin.homework_yjj_login.R;

public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sp = getSharedPreferences("firstStart", MODE_PRIVATE);
        boolean isFirst = sp.getBoolean("isFirst", true);
        if (isFirst) {
            //跳转到 引导页
            startActivity(new Intent(this, GuidanceActivity.class));
            finish();
        }
        setContentView(R.layout.activity_welcome);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        h.sendMessage(h.obtainMessage(1, 3, 0));
    }

    private void initView() {
        tv_show = findViewById(R.id.tv_show);
        tv_show.setOnClickListener(this);
    }

    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (msg.arg1 < 0) {
                        h.sendEmptyMessage(2);
                    } else {
                        String s = msg.arg1 + "s 后跳过";
                        tv_show.setText(s);
                        Message obtain = Message.obtain();
                        obtain.what = 1;
                        obtain.arg1 = msg.arg1 - 1;
                        h.sendMessageDelayed(obtain, 1500);
                    }
                    break;
                case 2:
                    go2Main();
                    break;
            }
            return false;
        }
    });

    private void go2Main() {
        //跳转到登录页面
        startActivity(new Intent(this, LoginActivity.class));
        finish();//结束自己
    }

    /**
     * 在欢迎页面的时候,屏蔽返回键按钮
     */
    @Override
    public void onBackPressed() {
//        super.onBackPressed();//覆盖掉父类的默认行为
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_show:
                //点击直接跳过广告,清空消息
                h.removeMessages(1);
                h.removeMessages(2);
                go2Main();
                break;
        }
    }
}
